package com.swd.order.service;

import com.swd.order.mapper.UserMapper;
import com.swd.order.model.User;
import com.swd.order.model.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User register(String opneId,String wxName,String photoUrl){

        User user = new User();
        user.setOpenid(opneId);
        user.setWxName(wxName);
        user.setPicture(photoUrl);
        user.setAdminister(false);
        user.setMealNotify(false);
        user.setOrderNotify(false);

        int insert = userMapper.insert(user);

        return insert == 1 ? user : null;
    }

    public User getUserByOpenId(String openId){

        UserExample userExample = new UserExample();
        userExample.createCriteria().andOpenidEqualTo(openId);
        List<User> users = userMapper.selectByExample(userExample);

        if (users.size()>0){
            return users.get(0);
        }else {
            return null;
        }

    }

//    public User queryByName(String name){
//
//
//
//    }


    public List<User> getCreateNotifyUser() {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andOrderNotifyEqualTo(true);
        return userMapper.selectByExample(userExample);
    }

    public List<User> getMealNotifyUser() {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andMealNotifyEqualTo(true);
        return userMapper.selectByExample(userExample);
    }

    public List<User> queryAll() {
        return userMapper.selectByExample(new UserExample());

    }

    public User queryById(Integer id) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdEqualTo(id);

        List<User> users = userMapper.selectByExample(userExample);

        if (users.size() ==0){
            return null;
        }

        return users.get(0);
    }


    public boolean update(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        return i==1;
    }
}
